package com.easylinkin.linkappapi.operatelog.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.application.service.ApplicationService;
import com.easylinkin.linkappapi.operatelog.entity.VisitPrivilegeLog;
import com.easylinkin.linkappapi.operatelog.mapper.VisitPrivilegeLogMapper;
import com.easylinkin.linkappapi.operatelog.service.VisitPrivilegeLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappPrivilege;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.security.mapper.LinkappPrivilegeMapper;
import com.easylinkin.linkappapi.security.service.LinkappPrivilegeService;
import com.easylinkin.sm.entity.Privilege;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 访问菜单日志 服务实现类
 * </p>
 *
 * @author Xiaoyang
 * @since 2022-11-23
 */
@Service
public class VisitPrivilegeLogServiceImpl extends
    ServiceImpl<VisitPrivilegeLogMapper, VisitPrivilegeLog> implements VisitPrivilegeLogService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  @Resource
  private LinkappPrivilegeMapper linkappPrivilegeMapper;

  @Override
  public boolean add(VisitPrivilegeLog visitPrivilegeLog) {
    visitPrivilegeLog.setCreatorId(linkappUserContextProducer.getCurrent().getId());
    visitPrivilegeLog.setCreateTime(new Date());
    visitPrivilegeLog.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    LinkappPrivilege linkappPrivilege = linkappPrivilegeMapper
        .selectByCode(visitPrivilegeLog.getPrivilegeCode());
    if (null != linkappPrivilege) {
      visitPrivilegeLog.setName(linkappPrivilege.getName());
      return save(visitPrivilegeLog);
    }
    return false;
  }

  @Override
  public IPage<VisitPrivilegeLog> getPage(Page page, VisitPrivilegeLog visitPrivilegeLog) {
    visitPrivilegeLog.setCreatorId(linkappUserContextProducer.getCurrent().getId());
    visitPrivilegeLog.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    Set<LinkappPrivilege> linkappPrivilegeList = getPrivilege();
    Set<String> codes = null;
    if (CollectionUtil.isNotEmpty(linkappPrivilegeList)) {
      codes = linkappPrivilegeList.stream().map(LinkappPrivilege::getCode)
          .collect(Collectors.toSet());
    }
    return baseMapper.getPage(page, visitPrivilegeLog, codes);
  }

  @Autowired
  ApplicationService applicationService;

  @Autowired
  LinkappPrivilegeService privilegeService;

  private Set<LinkappPrivilege> getPrivilege() {
    LinkappUser user = linkappUserContextProducer.getCurrent();
    Set<LinkappPrivilege> list = new TreeSet<LinkappPrivilege>();
    if ("1".equals(user.getType())) {
      List<LinkappPrivilege> privileges = applicationService
          .selectApplicationPrivilegeByUser(user.getId().toString(), null);
      for (LinkappPrivilege privilege : privileges) {
        if (privilege.getType() == 1) {
          list.add(privilege);
        }
      }
    } else {
      TreeSet<LinkappPrivilege> privileges = privilegeService
          .selectPrivilegeByUser(user.getId().toString(), null);
      for (LinkappPrivilege privilege : privileges) {
        if (privilege.getType() == 1) {
          list.add(privilege);
        }
      }
    }
    return list;
  }
}
